import 'package:aweb5/util/NaviUtil.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:aweb5/pages/my_material.dart';

import 'package:aweb5/style.dart';
import 'package:aweb5/pages/pub_material.dart';
import 'package:aweb5/generated/json/material_list_entity_helper.dart';
import 'package:aweb5/generated/json/material_type_entity_helper.dart';
import 'package:aweb5/bean/aplus/material_list_entity.dart';
import 'package:aweb5/bean/aplus/material_type_entity.dart';
import 'package:aweb5/util/HttpClient.dart';
import 'package:load/load.dart';
import 'package:aweb5/Constants.dart';
import 'package:aweb5/config/Config.dart';
import 'package:aweb5/util/toast_util.dart';
import 'package:aweb5/businuss/sucai_pub_widget.dart';
class MaterialLib extends StatefulWidget {
  @override
  _MaterialLibState createState() => _MaterialLibState();
}

class _MaterialLibState extends State<MaterialLib> {
  bool pub = true;

  TextEditingController controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.white,
          title: Row(
            children: <Widget>[

//              GestureDetector(
//                child:Container(
//                  alignment: Alignment.centerLeft,
//                  width: ScreenUtil().setWidth(39),
//                  height: ScreenUtil().setHeight(39),
//                  child: Text(""),
//                ) ,
//                onTap: (){
//
////                  NaviUtil.pop(context);
//
//                },
//
//              )
//              ,
              Container(
                alignment: Alignment.center,
                width: ScreenUtil().setWidth(367),
                height: ScreenUtil().setHeight(39),
                decoration: BoxDecoration(
                    color: Color(0xfff1f1f1),
                    borderRadius: BorderRadius.circular(5)),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      width: ScreenUtil().setWidth(20),
                    ),
                    Expanded(
                      child: TextField(
                        decoration: InputDecoration(
                            hintText: "输入团品或关键字搜索",
                            hintStyle: TextStyle(color: Color(0xffa3a3a3)),
                            border: InputBorder.none,
                            contentPadding: EdgeInsets.only(bottom: 12)),
                        controller: controller,
                      ),
                    ),
                    GestureDetector(
                      child: Image.asset(
                        "assets/search1.png",
                        width: ScreenUtil().setWidth(22),
                        height: ScreenUtil().setHeight(22),
                      ),
                      onTap: (){

                        if(pub){
                          pubkey.currentState.setSearchWord(controller.text.toString());
                          pubkey.currentState.requestMaterial(false);
                        }else{
                          mykey.currentState.setSearchWord(controller.text.toString());
                          mykey.currentState.requestMaterial();
                        }

                      },
                    ),

                    SizedBox(
                      width: ScreenUtil().setWidth(18),
                    )
                  ],
                ),
              ),
            ],
          ),
      elevation: 0,
      ),
      body: SafeArea(
          child: Container(
        padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(19),
            right: ScreenUtil().setWidth(19),
            top: ScreenUtil().setHeight(9)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(
              height: ScreenUtil().setHeight(23),
            ),
            buildTab(),
            SizedBox(
              height: ScreenUtil().setHeight(12),
            ),
            Expanded(child: buildBody())

//            Expanded(child: buildGrid())
          ],
        ),
      )),
    );
  }

  GlobalKey<MyMaterialState> mykey = GlobalKey();
  GlobalKey<PubMaterialState> pubkey = GlobalKey();
  Widget buildBody() {
    if (pub) {
      return PubMaterial(controller.text.toString(),pubkey);
    } else {
      return MyMaterial(controller.text.toString(),mykey);
//      return Text("");

    }
  }

  Widget buildTab() {
    return Row(
      children: <Widget>[
        GestureDetector(
          child: Container(
            width: ScreenUtil().setWidth(179),
            height: ScreenUtil().setHeight(40),
            child: Text(
              "素材库",
              style: TextStyle(
                  color: pub?Colors.white:Color(0xffF94F68), fontSize: ScreenUtil().setSp(18)),
            ),
            decoration: pub?redButtonStyle:redButtonStyleWithBorder,
            alignment: Alignment.center,
          ),
          onTap: () {
            pub = true;
            setState(() {});
          },
        ),
        SizedBox(
          width: ScreenUtil().setWidth(19),
        ),
        GestureDetector(
          child: Container(
            width: ScreenUtil().setWidth(178),
            height: ScreenUtil().setHeight(40),
            child: Text(
              "我的素材包",
              style: TextStyle(
                  color: pub?Color(0xffF94F68):Colors.white, fontSize: ScreenUtil().setSp(18)),
            ),
            decoration: pub?redButtonStyleWithBorder:redButtonStyle,
            alignment: Alignment.center,
          ),
          onTap: () {
            pub = false;
            setState(() {});
          },
        ),
      ],
    );
  }

  TextStyle b() {
    return TextStyle(
        color: Color(0xff101010),
        fontSize: ScreenUtil().setSp(16),
        fontWeight: FontWeight.bold);
  }

  TextStyle b1() {
    return TextStyle(
        color: Color(0xff101010), fontSize: ScreenUtil().setSp(14));
  }

  TextStyle grey() {
    return TextStyle(
        color: Color(0xffa7a7a7), fontSize: ScreenUtil().setSp(14));
  }

//  Widget buildGrid(){
//
//
//    return GridView.builder(gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
//      maxCrossAxisExtent: ScreenUtil().setWidth(130), //单个子widget的水平最大宽度
//      mainAxisSpacing: ScreenUtil().setWidth(32), //水平子widget的间距
//      crossAxisSpacing: ScreenUtil().setHeight(22), //垂直间距
//      childAspectRatio: 0.7,
//    ), itemBuilder: (con,index){
//
//      return Column(children: <Widget>[
//
//        Image.asset("assets/ab.png",width: ScreenUtil().setWidth(55),height: ScreenUtil().setHeight(55),),
//        SizedBox(height: ScreenUtil().setHeight(6),),
//        Text("四川青群扇贝肉加量加大包",style: b1(),maxLines: 2,overflow: TextOverflow.ellipsis,),
//        SizedBox(height: ScreenUtil().setHeight(6),),
//        Text("7.22 15:37",style: grey(),),
//        SizedBox(height: ScreenUtil().setHeight(6),),
//        Row(children: <Widget>[
//          Text("7.22 15:37",style: grey(),),
//          SizedBox(width: ScreenUtil().setWidth(6),),
//          Text("已上架",style: TextStyle(color: Color(0xffF44336),fontSize: ScreenUtil().setSp(12)),)
//
//
//        ],)
//
//
//      ],);
//
//    });
//
//
//  }

  Widget buildSearch() {}
}
